package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.JLabel;

import Controller.CompanyManagerController;
import Entity.ComplaintReport;
import Entity.DisabledReport;
import Entity.OrderReport;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * this is the GUI for view reports.
 * @author Shahar Shelly
 *
 */
public class ViewReports extends JPanel {

	private JTable table;
	private JTable table2;
	private JTable table3;
	JPanel cur=this;
	JFrame frame;
	JPanel lastPanel;
	public void setLastPanel (JPanel panel)
	{
		this.lastPanel=panel;
	}
	public void setFrame (JFrame frame)
	{
		this.frame=frame;
	}

	/**
	 *  is the constructor. it has a table to chose from and save after validate.
	 */
	public ViewReports(OrderReport[] o,ComplaintReport[] c,DisabledReport[] d)
	{
		final OrderReport[] o2=o;
		final ComplaintReport[] c2=c;
		final DisabledReport[] d2=d;
		setBounds(100, 100, 833, 429);
		setBorder(new EmptyBorder(5, 5, 5, 5));
		setLayout(null);
		
		JLabel lblOrdersReports = new JLabel("Orders Reports:");
		lblOrdersReports.setBounds(362, 11, 119, 14);
		add(lblOrdersReports);
		
		table = new JTable(new OrdersReport(o));
		table.setBounds(270, 64, 241, 124);
		JScrollPane sp=new JScrollPane(table);
		sp.setBounds(372, 36, 209, 82);
		add(sp);
		
		table.addMouseListener(new MouseAdapter() {
			  public void mouseClicked(MouseEvent e) {
			    if (e.getClickCount() == 2) {
			      JTable target = (JTable)e.getSource();
			      int row = target.getSelectedRow();
			      ViewOrdersReport vor=new ViewOrdersReport(o2[row]);
			      vor.setFrame(frame);
			      vor.setLastPanel(cur);
					vor.setVisible(true);
			    }
			  }
		});
		
		table2 = new JTable(new ComplaintsReport(c));
		JScrollPane sp2=new JScrollPane(table2);
		sp2.setBounds(372,150, 209, 82);
		add(sp2);
		
		JLabel lblComplaintReport = new JLabel("Complaints' Status Reports:");
		lblComplaintReport.setBounds(362, 129, 192, 14);
		add(lblComplaintReport);
		
		JLabel lblDisabledPlaceReports = new JLabel("Disabled Places Reports:");
		lblDisabledPlaceReports.setBounds(362, 243, 160, 14);
		add(lblDisabledPlaceReports);
		
		table2.addMouseListener(new MouseAdapter() {
			  public void mouseClicked(MouseEvent e) {
			    if (e.getClickCount() == 2) {
			      JTable target = (JTable)e.getSource();
			      int row = target.getSelectedRow();
			      ViewComplaintStatusReport n=new ViewComplaintStatusReport(c2[row]);
			      n.setFrame(frame);
			      n.setLastPanel(cur);
					n.setVisible(true);
			      
			    }
			  }
		});
		
		table3 = new JTable(new DisabledsReport(d));
		JScrollPane sp3=new JScrollPane(table3);
		sp3.setBounds(372, 269, 209, 82);
		add(sp3);
		
		JButton btnReturn = new JButton("Return");
		btnReturn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setContentPane((JPanel)lastPanel);
			}
		});
		btnReturn.setBounds(433, 372, 89, 23);
		add(btnReturn);
		
		table3.addMouseListener(new MouseAdapter() {
			  public void mouseClicked(MouseEvent e) {
			    if (e.getClickCount() == 2) {
			      JTable target = (JTable)e.getSource();
			      int row = target.getSelectedRow();
			      ViewDisabledReport r=new ViewDisabledReport(d2[row]);
			      r.setFrame(frame);
			      r.setLastPanel(cur);
					r.setVisible(true);
			      
			    }
			  }
		});
	}
}

class DisabledsReport extends AbstractTableModel
{
	String[] titles={"Parking Lot","End Date"};
	Object[][] data;
	
	public DisabledsReport(DisabledReport[] c)
	{
		super();
		this.data=new Object[c.length][2];
		for(int i=0; i<c.length; i++)
		{
			this.data[i][1]=c[i].getEndDate();
			this.data[i][0]=c[i].getParkingLot();
		}
	}
	@Override
	public int getColumnCount() {
		return this.titles.length;
	}

	@Override
	public int getRowCount() {
		return data.length;
	}

	@Override
	public Object getValueAt(int arg0, int arg1) {
		return data[arg0][arg1];
	}
	
	@Override
	public String getColumnName(int column) {
		// TODO Auto-generated method stub
		return (String) this.titles[column];
	}
}

class OrdersReport extends AbstractTableModel
{
	String[] titles={"Parking Lot","End Date"};
	Object[][] data;
	
	public OrdersReport(OrderReport[] c)
	{
		super();
		this.data=new Object[c.length][2];
		for(int i=0; i<c.length; i++)
		{
			this.data[i][1]=c[i].getEndDate();
			this.data[i][0]=c[i].getParkingLot();
		}
	}
	@Override
	public int getColumnCount() {
		return this.titles.length;
	}

	@Override
	public int getRowCount() {
		return data.length;
	}

	@Override
	public Object getValueAt(int arg0, int arg1) {
		return data[arg0][arg1];
	}
	
	@Override
	public String getColumnName(int column) {
		// TODO Auto-generated method stub
		return (String) this.titles[column];
	}
}

class ComplaintsReport extends AbstractTableModel
{
	String[] titles={"Parking Lot","End Date"};
	Object[][] data;
	
	public ComplaintsReport(ComplaintReport[] c)
	{
		super();
		this.data=new Object[c.length][2];
		for(int i=0; i<c.length; i++)
		{
			this.data[i][1]=c[i].getEndDate();
			this.data[i][0]=c[i].getParkingLot();
		}
	}
	@Override
	public int getColumnCount() {
		return this.titles.length;
	}

	@Override
	public int getRowCount() {
		return data.length;
	}

	@Override
	public Object getValueAt(int arg0, int arg1) {
		return data[arg0][arg1];
	}
	
	@Override
	public String getColumnName(int column) {
		// TODO Auto-generated method stub
		return (String) this.titles[column];
	}
}
